package com.lions.client.view.mappanel;

import com.lions.client.model.Module;
import com.lions.client.model.Type;

public class MapMatrix {
	private final Type[] modules;
	private final int numRows, numCols;
	
//	public MapMatrix(Model model) {
//		
//		model.getModuleList();
//		
//	}
	
	public MapMatrix(final int numRows, final int numCols) {
		this.numRows = numRows;
		this.numCols = numCols;
		this.modules = new Type[numRows * numCols];
	}

	/**
	 * Gets the number of rows in this matrix
	 * 
	 * @return
	 */
	public int getRows() {
		return this.numRows;
	}

	/**
	 * Gets the number of columns in this matrix
	 * 
	 * @return
	 */
	public int getCols() {
		return this.numCols;
	}

	/**
	 * Gets information on the type of module at this location
	 * 
	 * @param row
	 * @param col
	 * @return the type of module in this cell
	 */
	public Type getModule(final int row, final int col) {
		return modules[row * numCols + col];
	}

	/**
	 * Places this type of module in the specified location
	 * 
	 * @param row the row to be placed in
	 * @param col the column to be placed in
	 * @param type the type of module to be placed
	 */
	public void setModule(final int row, final int col, final Type type) {
		modules[row * numCols + col] = type;
	}
	
	/**
	 * Checks to see if the given module can be placed at the specified position
	 * 																	
	 * @param row the row to place the module in						|-----|-----|-----|
	 * @param col the column to place the module in						|-1,-1|-1,C |-1,+1|
	 * 																	|-----|-----|-----|
	 * @param module the module to check								| R,-1| R,C | R,+1|
	 * 																	|-----|-----|-----|	
	 * @return true if placeable, false if not							|+1,-1|+1,C |+1,+1|
	 * 																	|-----|-----|-----|
	 */
	public boolean isPlaceable(final int row, final int col, final Module module) {
		boolean placeable = false;
		switch (module.getType()) {
		case AIRLOCK:
			// Not next to dormitory
			if((getModule(row-1, col-1) == Type.DORMITORY) ||
				(getModule(row-1, col-1) != null)){
				placeable = false;
			}
			else if((getModule(row-1, col) == Type.DORMITORY) ||
					(getModule(row-1, col) != null)){
				placeable = false;
			}
			else if((getModule(row-1, col+1) == Type.DORMITORY) ||
					(getModule(row-1, col+1) != null)){
				placeable = false;
			}
			else if((getModule(row, col-1) == Type.DORMITORY) ||
					(getModule(row, col-1) != null)){
				placeable = false;
			}
			else if((getModule(row, col+1) == Type.DORMITORY) ||
					(getModule(row, col+1) != null)){
				placeable = false;
			}
			else if((getModule(row+1, col-1) == Type.DORMITORY) ||
					(getModule(row+1, col-1) != null)){
				placeable = false;
			}
			else if((getModule(row+1, col) == Type.DORMITORY) ||
					(getModule(row+1, col) != null)){
				placeable = false;
			}
			else if((getModule(row+1, col+1) == Type.DORMITORY) ||
					(getModule(row+1, col+1) != null)){
				placeable = false;
			}
			
		case CONTROL:
			return true;
			
		case POWER:
			return true;
			
		case FOOD_WATER_STORAGE:
			// Not next to sanitation
			// Within 3 spaces of canteen
			if((getModule(row-1, col-1) == Type.SANITATION) ||
					(getModule(row-1, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row-1, col) == Type.SANITATION) ||
						(getModule(row-1, col) != null)){
					placeable = false;
				}
				else if((getModule(row-1, col+1) == Type.SANITATION) ||
						(getModule(row-1, col+1) != null)){
					placeable = false;
				}
				else if((getModule(row, col-1) == Type.SANITATION) ||
						(getModule(row, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row, col+1) == Type.SANITATION) ||
						(getModule(row, col+1) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col-1) == Type.SANITATION) ||
						(getModule(row+1, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col) == Type.SANITATION) ||
						(getModule(row+1, col) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col+1) == Type.SANITATION) ||
						(getModule(row+1, col+1) != null)){
					placeable = false;
				}
			
		case DORMITORY:
			// Not next to airlock
			if((getModule(row-1, col-1) == Type.AIRLOCK) ||
					(getModule(row-1, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row-1, col) == Type.AIRLOCK) ||
						(getModule(row-1, col) != null)){
					placeable = false;
				}
				else if((getModule(row-1, col+1) == Type.AIRLOCK) ||
						(getModule(row-1, col+1) != null)){
					placeable = false;
				}
				else if((getModule(row, col-1) == Type.AIRLOCK) ||
						(getModule(row, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row, col+1) == Type.AIRLOCK) ||
						(getModule(row, col+1) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col-1) == Type.AIRLOCK) ||
						(getModule(row+1, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col) == Type.AIRLOCK) ||
						(getModule(row+1, col) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col+1) == Type.AIRLOCK) ||
						(getModule(row+1, col+1) != null)){
					placeable = false;
				}
			
		case CANTEEN:
			// Not next to sanitation
			// Within 3 of storage
			if((getModule(row-1, col-1) == Type.SANITATION) ||
					(getModule(row-1, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row-1, col) == Type.SANITATION) ||
						(getModule(row-1, col) != null)){
					placeable = false;
				}
				else if((getModule(row-1, col+1) == Type.SANITATION) ||
						(getModule(row-1, col+1) != null)){
					placeable = false;
				}
				else if((getModule(row, col-1) == Type.SANITATION) ||
						(getModule(row, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row, col+1) == Type.SANITATION) ||
						(getModule(row, col+1) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col-1) == Type.SANITATION) ||
						(getModule(row+1, col-1) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col) == Type.SANITATION) ||
						(getModule(row+1, col) != null)){
					placeable = false;
				}
				else if((getModule(row+1, col+1) == Type.SANITATION) ||
						(getModule(row+1, col+1) != null)){
					placeable = false;
				}
			
		case SANITATION:
			// Not next to canteen or storage
			// Adjacent to gym
			if((getModule(row-1, col-1) == Type.CANTEEN) ||
				(getModule(row-1, col-1) == Type.FOOD_WATER_STORAGE) ||
				(getModule(row-1, col-1) != null)){
				placeable = false;
			}
			else if((getModule(row-1, col) == Type.CANTEEN) ||
					(getModule(row-1, col) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row-1, col) != null)){
				placeable = false;
			}
			else if((getModule(row-1, col+1) == Type.CANTEEN) ||
					(getModule(row-1, col+1) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row-1, col+1) != null)){
				placeable = false;
			}
			else if((getModule(row, col-1) == Type.CANTEEN) ||
					(getModule(row, col-1) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row, col-1) != null)){
				placeable = false;
			}
			else if((getModule(row, col+1) == Type.CANTEEN) ||
					(getModule(row, col+1) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row, col+1) != null)){ 
				placeable = false;
			}
			else if((getModule(row+1, col-1) == Type.CANTEEN) ||
					(getModule(row+1, col-1) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row+1, col-1) != null)){
				placeable = false;
			}
			else if((getModule(row+1, col) == Type.CANTEEN) ||
					(getModule(row+1, col) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row+1, col) != null)){
				placeable = false;
			}
			else if((getModule(row+1, col+1) == Type.CANTEEN) ||
					(getModule(row+1, col+1) == Type.FOOD_WATER_STORAGE) ||
					(getModule(row+1, col+1) != null)){
				placeable = false;
			}
			
			
		case GYM:
			// Adjacent to sanitation
			Type type1 = getModule(row,col-1);
			Type type2 = getModule(row,col+1);
			if ((type1 == Type.SANITATION) || (type1 == null) ||
					(type2 == Type.SANITATION) || (type2 == null)){
				placeable = true;
			}	
			
		case MEDICAL:
			placeable = true;
			
		case PLAIN:
			placeable = true;
		}
		return placeable;
	}
}
